﻿using System.Collections.Generic;
using System.Linq;
using Coder.Workflow;
using Fee.FileAttachs;
using Identity;

namespace Fee.Orders.DesignWorkflow
{
    /// <summary>
    ///     设计预算委托订单
    /// </summary>
    public class BudgetOrder : DelegateOrderBase

    {
        public const string DesignPlanCostWorkflowName = "设计初步预算流程";
        public const string DesignPlanCostStep1 = "预算编写";
        public const string DesignPlanCostPlanStep3 = "工程建设中心审核";
        public const string DesignPlanCostStep2 = "分公司确认";
        private IList<BudgetOrderFileAttach> _attaches;
        private IList<BudgetOrderPlanCost> _planCosts;


        public BudgetOrder(WorkProcess wrWorkProcess, User user) : base(wrWorkProcess, user)
        {
        }

        protected BudgetOrder()
        {
        }

        public IList<BudgetOrderPlanCost> PlanCosts
        {
            get => _planCosts ?? (_planCosts = new List<BudgetOrderPlanCost>());
            set => _planCosts = value;
        }

        public IList<BudgetOrderFileAttach> Attaches
        {
            get => _attaches ?? (_attaches = new List<BudgetOrderFileAttach>());
            set => _attaches = value;
        }

        /// <summary>
        /// </summary>
        /// <returns></returns>
        public override void Complete()
        {
            base.Complete();

            // 更新预算

            var result = ConstructionTask.PlanCosts.ToDictionary(f => f.Type, f => f);

            foreach (var orderPlanCost in PlanCosts)
            {
                ConstructionTaskPlanCost taskPlanCost = null;
                if (!result.ContainsKey(orderPlanCost.Type))
                {
                    taskPlanCost = new ConstructionTaskPlanCost
                    {
                        Type = orderPlanCost.Type,
                        ProcessInfo = { ConstructionOperator = ConstructionOperator.Added }
                    };
                }
                else
                {
                    taskPlanCost = result[orderPlanCost.Type];
                    taskPlanCost.ProcessInfo.ConstructionOperator = ConstructionOperator.Changed;
                }

                taskPlanCost.UpdateBy(orderPlanCost);
                taskPlanCost.ProcessInfo.Number = Number;

                ConstructionTask.PlanCosts.Add(taskPlanCost);
            }


            // 跟新供应商
            var vendorInfo =
                ConstructionTask.Details.ToDictionary(f => f.ContractType.ToString() + f.ConstructionType, f => f);
            foreach (var detail in Details)
            {
                 
                var taskDetail = vendorInfo[detail.ContractType.ToString() + detail.ConstructionType];
                taskDetail.Operator = detail.Operator;
                if (taskDetail.UpdateBy(detail))
                {
                    
                    taskDetail.ProcessInfo.ConstructionOperator = detail.Operator;
                    taskDetail.ProcessInfo.Number = Number;
                }

            }
            //更新附件,由于这里无法有filemanager,因此再manager处理

            ConstructionTask.Status = ConstructionTaskStatus.Designed;
        }

        public void UpdateBy(IEnumerable<BudgetOrderPlanCost> dataPlanCosts)
        {
            var updateDate = dataPlanCosts.ToDictionary(
                f => f.Type,
                f => f
            );

            foreach (var item in PlanCosts)
                if (updateDate.ContainsKey(item.Type))
                    item.UpdateBy(updateDate[item.Type]);
        }
    }
}